#include "GMutex.h"
#include "GError.h"

namespace GThread
{
	// Description: Constructor
	// Return:
	// Notes:
	// -----------------------------------------------------
	GMutex::GMutex()
	{
		Mutex = NULL;
		Name = "";
	}

	// Description: Destructor
	// Return:
	// Notes: Releases the mutex
	// -----------------------------------------------------
	GMutex::~GMutex()
	{
		Release();
	}

	// Description: Resurns the actual handle of the mutex
	// Return:
	// Notes:
	// -----------------------------------------------------
	void* GMutex::GetHandle() const
	{
		return Mutex;
	}

	// Description: Returns the name of the mutex
	// Return:
	// Notes:
	// -----------------------------------------------------
	const char* GMutex::GetName() const
	{
		return Name.c_str();
	}

	// Description: Returns if the event object is created.
	// Return: true if created, false otherwise
	// Notes:
	// -----------------------------------------------------
	bool GMutex::IsCreated() const
	{
		return Mutex != NULL;
	}

	// Description: Acquires the ownership of the mutex. Uses GSynchronizer::WaitFor method.
	// Return: returns GWaitResult based on the result of function call.
	// Notes: Mutex must be creted before calling.
	// -----------------------------------------------------
	GWaitResult GMutex::Acquire(GUInt32 TimeOut)
	{
		GUtil::GError::DebugCheck(!IsCreated(), "Mutex is not created.");

		return GSynch::WaitFor(this, TimeOut);
	}

}